package com.example.myapplication2;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.Calendar;

public class DatePickerDialogActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener, DatePicker.OnDateChangedListener {
    TextView textView;
    DatePicker datePicker;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker_dialog);
        DatePicker datePicker2 = findViewById(R.id.DatePicker2);
        //设置监听
        datePicker2.setOnDateChangedListener(this);

        findViewById(R.id.DatePickerButton).setOnClickListener(this);
        findViewById(R.id.DatePickerButton2).setOnClickListener(this);
        datePicker = findViewById(R.id.DatePicker);
        textView = findViewById(R.id.DatePickerDialogText);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.DatePickerButton:
                textView.setText("选中.."+datePicker.getYear()+""+datePicker.getMonth()+1+""+datePicker.getDayOfMonth());
                break;
            case R.id.DatePickerButton2:
                //创建弹窗日历控件
                Calendar calendar=Calendar.getInstance();
                DatePickerDialog datePickerDialog = new DatePickerDialog(this,this,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1,calendar.get(Calendar.DAY_OF_MONTH));
                //显示
                datePickerDialog.show();
                break;
        }
    }

    @Override
    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
        textView.setText("弹窗选中.."+datePicker.getYear()+""+datePicker.getMonth()+""+datePicker.getDayOfMonth());
    }

    @Override
    public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
        textView.setText("日历控件选中.."+datePicker.getYear()+""+datePicker.getMonth()+1+""+datePicker.getDayOfMonth());
    }
}